package com.example1.demo.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;

import java.io.File;
import java.io.IOException;

import org.springframework.web.bind.annotation.PostMapping;


@RestController
public class upFile1 {
 
    @PostMapping("/upload1")
    public String upload(String nickname,MultipartFile photo,HttpServletRequest request) throws IllegalStateException, IOException {

        System.out.println(nickname);
        System.out.println(photo.getOriginalFilename());
        System.out.println(photo.getContentType());
        String path = request.getServletContext().getRealPath("/upload1/");
        System.out.println(path);
        saveFile(photo,path);
        return "上传成功";
    }

    public void saveFile(MultipartFile photo, String path) throws IllegalStateException, IOException {
    
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();
        }
        File file = new File(path+photo.getOriginalFilename());
        photo.transferTo(file);
    }
    
}
